package cn.stylefeng.guns.modular.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.business.entity.TrainingRecordRequest;
import cn.stylefeng.guns.modular.business.enums.TrainingStatusEnum;
import cn.stylefeng.guns.modular.business.mapper.TrainingRecordMapper;
import cn.stylefeng.guns.modular.business.pojo.TrainingRecord;
import cn.stylefeng.guns.modular.business.service.TrainingRecordService;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.system.modular.user.entity.SysUser;
import cn.stylefeng.roses.kernel.system.modular.user.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class TrainingRecordServiceImpl extends ServiceImpl<TrainingRecordMapper, TrainingRecord> implements TrainingRecordService {

    @Resource
    SysUserService sysUserService;

    @Override
    public boolean edit(TrainingRecordRequest trainingRecordRequest) {
        // 获取当前登录用户
        LoginUser loginUser = LoginContext.me().getLoginUser();

        TrainingRecord record = new TrainingRecord();
        BeanUtil.copyProperties(trainingRecordRequest, record);
        // 设置更新时间 和 更新人
        record.setUpdateTime(new Date());
        record.setUpdateUser(loginUser.getUserId());
        record.setStatusFlag(TrainingStatusEnum.CREATED);
        return this.updateById(record);
    }

    @Override
    public void add(TrainingRecordRequest trainingRecordRequest) {
        // 获取当前登录用户
        LoginUser loginUser = LoginContext.me().getLoginUser();
        // 获取当前用户详细信息
        SysUser sysUser = sysUserService.getById(loginUser.getUserId());

        TrainingRecord record = new TrainingRecord();
        BeanUtil.copyProperties(trainingRecordRequest, record);
        // 设置培训记录的人员信息
        record.setUserId(loginUser.getUserId());
        record.setRealName(sysUser.getRealName());
        record.setNickName(sysUser.getNickName());
        record.setAccount(sysUser.getAccount());
        record.setCreateUser(loginUser.getUserId());
        record.setCreateTime(new Date());
        record.setStatusFlag(TrainingStatusEnum.CREATED);
        this.save(record);
    }

    @Override
    public PageResult<TrainingRecord> findPage(TrainingRecordRequest trainingRecordRequest) {
        LambdaQueryWrapper<TrainingRecord> wrapper = this.createWrapper(trainingRecordRequest);

        Page<TrainingRecord> page = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(page);
    }

    @Override
    public TrainingRecord findOneById(Long recordId) {
        if (recordId == null) {
            return null;
        }
        return this.getById(recordId);
    }

    @Override
    public boolean audit(TrainingRecordRequest trainingRecordRequest) {
        // 获取当前登录用户
        LoginUser loginUser = LoginContext.me().getLoginUser();

        TrainingRecord record = new TrainingRecord();
        record.setRecordId(trainingRecordRequest.getRecordId());
        record.setStatusFlag(trainingRecordRequest.getStatusFlag());
        // 设置更新时间 和 更新人
        record.setUpdateTime(new Date());
        record.setUpdateUser(loginUser.getUserId());
        return this.updateById(record);
    }

    /**
     * 创建查询sql
     * @param trainingRecordRequest 请求参数
     * @return sql查询对象
     */
    private LambdaQueryWrapper<TrainingRecord> createWrapper(TrainingRecordRequest trainingRecordRequest) {
        LambdaQueryWrapper<TrainingRecord> queryWrapper = new LambdaQueryWrapper<>();

        // 根据排序升序排列，序号越小越在前
        queryWrapper.orderByAsc(TrainingRecord::getRecordId);

        if (ObjectUtil.isEmpty(trainingRecordRequest)) {
            return queryWrapper;
        }

        //根据账号和开始结束时间进行查询
        String account = trainingRecordRequest.getAccount();

        // SQL条件拼接
        queryWrapper.eq(!StringUtils.isEmpty(account), TrainingRecord::getAccount, account);
//        queryWrapper.eq(!StringUtils.isEmpty(attendanceDay), AttendanceRecord::getAttendanceDay, attendanceDay);
//        queryWrapper.eq(ObjectUtil.isNotNull(userId), AttendanceRecord::getUserId, userId);
//        queryWrapper.ge(ObjectUtil.isNotNull(startTime), AttendanceRecord::getStartTime, startTime);
//        queryWrapper.lt(ObjectUtil.isNotNull(endTime), AttendanceRecord::getEndTime, endTime);

        return queryWrapper;
    }
}
